package com.ruoyi.system.service.impl;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.JiajiaMallShopInformation;
import com.ruoyi.system.mapper.JiajiaMallShopInformationMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.IJiajiaMallShopInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

/**
 * 商城店铺信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-03-05
 */
@Service
public class JiajiaMallShopInformationServiceImpl implements IJiajiaMallShopInformationService 
{
    @Autowired
    private JiajiaMallShopInformationMapper jiajiaMallShopInformationMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询商城店铺信息
     * 
     * @param id 商城店铺信息主键
     * @return 商城店铺信息
     */
    @Override
    public JiajiaMallShopInformation selectJiajiaMallShopInformationById(Long id)
    {
        return jiajiaMallShopInformationMapper.selectJiajiaMallShopInformationById(id);
    }

    /**
     * 查询商城店铺信息列表
     * 
     * @param jiajiaMallShopInformation 商城店铺信息
     * @return 商城店铺信息
     */
    @Override
    @DataScope(userAlias = "u" ,deptAlias = "d")
    public List<JiajiaMallShopInformation> selectJiajiaMallShopInformationList(JiajiaMallShopInformation jiajiaMallShopInformation)
    {
        return jiajiaMallShopInformationMapper.selectJiajiaMallShopInformationList(jiajiaMallShopInformation);
    }

    /**
     * 新增商城店铺信息
     * 
     * @param jiajiaMallShopInformation 商城店铺信息
     * @return 结果
     */
    @Override
    public int insertJiajiaMallShopInformation(JiajiaMallShopInformation jiajiaMallShopInformation)
    {
        //自动创建后台账号

        SysUser sysUser = sysUserMapper.selectUserByCertNo(jiajiaMallShopInformation.getLegalCertNo());
        if(Optional.ofNullable(sysUser).isPresent()){
            jiajiaMallShopInformation.setUserId(sysUser.getUserId());
            jiajiaMallShopInformation.setShopStatus("0");
            jiajiaMallShopInformation.setShopLevel("0");
            jiajiaMallShopInformation.setJoiningStartTime(LocalDateTime.now());
        }else{
            throw new ServiceException("请先注册用户");
        }
        return jiajiaMallShopInformationMapper.insertJiajiaMallShopInformation(jiajiaMallShopInformation);
    }

    /**
     * 修改商城店铺信息
     * 
     * @param jiajiaMallShopInformation 商城店铺信息
     * @return 结果
     */
    @Override
    public int updateJiajiaMallShopInformation(JiajiaMallShopInformation jiajiaMallShopInformation)
    {
        jiajiaMallShopInformation.setUpdateTime(DateUtils.getNowDate());
        return jiajiaMallShopInformationMapper.updateJiajiaMallShopInformation(jiajiaMallShopInformation);
    }

    /**
     * 批量删除商城店铺信息
     * 
     * @param ids 需要删除的商城店铺信息主键
     * @return 结果
     */
    @Override
    public int deleteJiajiaMallShopInformationByIds(String ids)
    {
        return jiajiaMallShopInformationMapper.deleteJiajiaMallShopInformationByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除商城店铺信息信息
     * 
     * @param id 商城店铺信息主键
     * @return 结果
     */
    @Override
    public int deleteJiajiaMallShopInformationById(Long id)
    {
        return jiajiaMallShopInformationMapper.deleteJiajiaMallShopInformationById(id);
    }
}
